package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.grantha.util.DB;
import com.grantha.util.Util;
/**
 * Servlet implementation class BookSearchServlet
 */
@WebServlet("/BookSearchServlet")
public class BookSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	Logger log = Logger.getLogger(this.getClass());
       
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		
		log.info("Servlet Stated");
		
		String queryName = request.getParameter("queryName");
		String searchType = request.getParameter("searchType");
		String searchColumn = request.getParameter("searchColumn");
		int length = queryName.length();
		
		if(queryName == ""){
		out.print(Util.sendJSONMessage(false, "the feild is required"));
		}
		
		else if(length < 3){
			out.print(Util.sendJSONMessage(false, "Need atleast three characters"));
		}
		
		log.info("queryName="+queryName);
		log.info("searchType="+searchType);
		log.info("searchColumn="+searchColumn);
		
		int searchTypeInt = Integer.parseInt(searchType);
		String searchQueryLastPart = "";
		
		switch (searchTypeInt) {
		case 0:
			searchQueryLastPart = " like ? ";
			queryName = queryName+"%";
			break;
		case 1:
			searchQueryLastPart = " = ? ";
			break;
		case 2:
			searchQueryLastPart = " like ? ";
			queryName = "%"+queryName;
			break;
		case 3:
			searchQueryLastPart = " like ? ";
			queryName = "%"+queryName+"%";
			break;
		}
		
		try {
			ResultSet rs =  DB.search("select BookNo, Author, Title, Date from BookRegister where "+searchColumn+searchQueryLastPart, queryName);
			JSONArray jArr = new JSONArray();
			JSONObject jObj ;
			while(rs.next()){
				jObj = new JSONObject();
				jObj.put("BookNo", rs.getString("BookNo"));
				jObj.put("Author", rs.getString("Author"));
				jObj.put("Title", rs.getString("Title"));
				jObj.put("Date", rs.getString("Date"));
				
				jArr.add(jObj);
				log.info("success");
			}
			
			out.write(jArr.toString());
			out.close();
			log.info("Servlet end");

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("Error");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
